<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//技师管理
class suggest extends My_Controller {
	
	function __construct(){
		parent::__construct();
		
	}
	//获取门店id
	public function get_id(){
		return rbac_conf(array('INFO','store_id'));
	}

	/**
	*	技师管理
	*/
	public function suggest_list(){
		$this->load->view('head.html');
		$this->load->view('suggest/suggest_list.html');
	}
	public function suggest_edit(){
		$showdata['store_name']=$this->product->get_store_name();
		/*$this->my_edit('suggest','suggest/suggest_edit.html',$showdata);*/
		$table='suggest';
		$view='suggest/suggest_edit.html';
		$data=$this->input->post();

        if($data){   //写入
        	$data['reply_user']=$this->get_nickname();
        	$data['reply_time']=time();
        	$data['status']=1;
        	
            $id=$data['id'];
            $this->db->update($table,$data,array('id'=>$id));
            $count=$this->db->affected_rows();
            if($count){
                echo '{"status":1, "msg": "数据更新成功"}';
            }else{
                echo '{"status":0, "msg": "没有数据被更新"}';
            }

        }else{  // 输出到页面
            $id=$_GET['id'];
            if($id){
                    $mysql="select * from $table where id =$id";
                    $res=$this->db->query($mysql)->result();
                    $data=$showdata;
                    $data['list']=$this->product->_show_time($res);
                    //var_dump($data);  
                    $this->load->view('head.html');
                    $this->load->view($view,$data);
            }else{
                echo    '{"status":0, "msg": "要修改的数据不存在"}';
            }
        }
        
	}
	public function suggest_show(){
		$data=$this->input->post();
		if($data){
			$this->db->where('id', $data['id']);
			$this->db->update('suggest', array('status'=>2));
			$count=$this->db->affected_rows();
			if($count){
                echo '{"status":1, "msg": "发布到小程序成功"}';
            }else{
                echo '{"status":0, "msg": "没有数据被更新"}';
            }
			
		}else{
			return false;
		}
		
	}
	public function suggest_store_list(){
		$this->load->view('head.html');
		$this->load->view('suggest/suggest_store_list.html');
	}
	
	public function suggest_store_add(){
		$data=$this->input->post();
		if($data){
			if($data['store_name']){
					$count = $this->product->_getcount('suggest_store', "store_name='".$data['store_name']."'");
					if($count)die('{"status":0, "msg": "门店名称已经存在"}');
			}				
			
			//$data['store_id']=$this->get_id();  //得到门店ID
			$this->db->insert('suggest_store', $data);
			echo '{"status":1, "msg": "添加门店成功"}';
		}else{
			
			//var_dump($data);
			$this->load->view('head.html');
			$this->load->view('suggest/suggest_store_add.html');
		}
		
	}

	public function suggest_store_del(){
		$this->my_del('suggest_store');
	}	

	public function suggest_store_edit(){
		$this->my_edit('suggest_store','suggest/suggest_store_edit.html');
	}

	/**
	*	技师等级管理
	*/
	public function tech_work_list(){
		$this->load->view('head.html');
		$this->load->view('store/tech/tech_work_list.html');
	}
	public function tech_work_add(){
		$data=$this->input->post();
		if($data){
			if($data['work_name']){
					$count = $this->product->_getcount('tech_work', "work_name='".$data['work_name']."'");
					if($count)die('{"status":0, "msg": "名称已经存在"}');
			}				
			
			$data['store_id']=$this->get_id();  //得到门店ID
			$this->db->insert('tech_work', $data);
			echo '{"status":1, "msg": "添加班次成功"}';
		}else{
			
			//var_dump($data);
			$this->load->view('head.html');
			$this->load->view('store/tech/tech_work_add.html');
		}
		
	}
	public function tech_work_del(){
		$this->my_del('tech_work');
	}	
	

	//技师排班
	public function tech_work_set(){
		$showdata['tech_list']=$this->product->get_tech_list();
		$table='tech_work';
		$view='store/tech/tech_work_set.html';
		$data=$this->input->post();
        if($data){   //写入
            $id=$data['id'];
            //$this->db->update($table,$data,array('id'=>$id));
            $serves=$data['serves']; //得到要更新的技师id
            if(empty($serves))die('{"status":0, "msg": "没有传递任何值"}');
            $mysql="update employee set work_id=$id where id in($serves)";
            $this->db->query($mysql);
            $count=$this->db->affected_rows();
            if($count){
                echo '{"status":1, "msg": "数据更新成功"}';
            }else{
                echo '{"status":0, "msg": "没有数据被更新"}';
            }

        }else{  // 输出到页面
            $id=$_GET['id'];
            if($id){
                    $mysql="select * from $table where id =$id";
                    $res=$this->db->query($mysql)->result();
                    $data=$showdata;
                    $data['list']=$this->product->_show_time($res);
                    //查询当前班次的技师
                    $store_id=$this->get_id();
                    $mysql="select id from employee where store_id=$store_id and emp_status=1 and work_id=$id";
                    $res=$this->db->query($mysql)->result();
       
                    $str='';
                    foreach ($res as $k => $v) {
                    	if(!empty($str)){
                    		$str.=','.$v->id;
                    	}else{
                    		$str.=$v->id;
                    	}
                    }
                    //将得到的技师ID 传递给前端
                    $data['list'][0]['serves']=$str;
                    $this->load->view('head.html');
                    $this->load->view($view,$data);
            }else{
                echo    '{"status":0, "msg": "要修改的数据不存在"}';
            }
        }
	}


	/**
	*	技师轮排方式管理
	*/
	public function turn_type_list(){
		$this->load->view('head.html');
		$this->load->view('store/tech/turn_type_list.html');
	}
	public function turn_type_add(){
		$data=$this->input->post();
		if($data){
			if($data['turn_type']){
					$count = $this->product->_getcount('tech_turn_type', "turn_type='".$data['turn_type']."'");
					if($count)die('{"status":0, "msg": "名称已经存在"}');
			}				
			
			$data['store_id']=$this->get_id();  //得到门店ID
			$this->db->insert('tech_turn_type', $data);
			echo '{"status":1, "msg": "添加轮排方式成功"}';
		}else{
			
			//var_dump($data);
			$this->load->view('head.html');
			$this->load->view('store/tech/turn_type_add.html');
		}
		
	}
	public function turn_type_del(){
		$this->my_del('tech_turn_type');
	}	
	public function turn_type_edit(){
		$showdata['tech_list']=$this->product->get_tech_list();
		$this->my_edit('tech_turn_type','store/tech/turn_type_edit.html',$showdata);
	}
}